//送出給劉陳小傳傳
function update_checkform_send_to_admin() {

    $("#front_man").val(1);
    $("#broker").val(1);

    if ($("#name").val() == "") {
      
        alert("請填寫客戶姓名");
        setTimeout(function() {
            $("#name").focus();
        }, 0);
        return false;

    } else if ($("#id_number").val() != "") {

        checkidnumber();

    } else {


        $("form#userinfo").hide();
        $("#relationship_info").show();
        $("form#contact_info").hide();
        $.ajax({
            type: "POST",
            url: '/show/relationship/update_relationship_submit',
            data: $("#relationship_info_form").serialize(),
            success: function(msg) {
            /////////////////////////////////////////////////////////////////////////
                $("form#userinfo").hide();
                $("#relationship_info").hide();
                $("form#contact_info").show();

                $.ajax({
                    type: "POST",
                    url: '/show/contact/data_update_submit',
                    data: $("form#contact_info").serialize(),
                    success: function(data) {
                    //----------------------------------------------------------------
                    $("form#userinfo").show();
                    $("#relationship_info").hide();
                    $("form#contact_info").hide();

                    $.ajax({
                        type: "POST",
                        url: '/client/data_edit_submit',
                        data: $("form#userinfo").serialize(),
                        success: function(data) {
                            //alert(data);
                            location.href = "/client/data_query";
                        }
                    });

                    //----------------------------------------------------------------
                    }
                });


            //////////////////////////////////////////////////
            }
        });



        

        
        
    }

    
}


//更新
function update_checkform() {


//alert('');

    if ($("#name").val() == "") {


        alert("請填寫客戶姓名");
        setTimeout(function() {
            $("#name").focus();
        }, 0);
        return false;


    } else {

        //alert('edit!!?!?!?!?!');
        $("#send_to_admin").hide();
        $("#update_checkform").hide();

        $("form#userinfo").hide();
        $("#relationship_info").show();
        $("form#contact_info").hide();
        $.ajax({
            type: "POST",
            url: '/show/relationship/update_relationship_submit',
            data: $("form#relationship_info_form").serialize(),
            success: function(msg) {


            
                $("form#userinfo").hide();
                $("#relationship_info").hide();
                $("form#contact_info").show();

                $.ajax({
                    type: "POST",
                    url: '/show/contact/data_update_submit',
                    data: $("form#contact_info").serialize(),
                    success: function(data) {
                        
                        
                        $("form#userinfo").show();
                        $("#relationship_info").hide();
                        $("form#contact_info").hide();

                        $.ajax({
                            type: "POST",
                            url: '/client/data_edit_submit',
                            data: $("form#userinfo").serialize(),
                            success: function(client_data) {
                                //alert('ur gg nn ');
                                alert('顧客編輯成功！');
                                location.href = "/client/data_query";
                            }
                        });
                    }
                });



            }
        });





        //alert('qqq');



    }

    //location.href = "/client/data_query_results";
}

$(document).ready(function() {


    $("#head").autocomplete({
                    source: function(request, response) {
                            $.ajax({
                                    url: "/client/users_josn",
                                    dataType: "json",
                                    data: {
                    query: request.term
                                    },
                                    success: function(data) {
                                            response($.map(data, function(item) {
                                                return {
                            label: item.name,
                            value: item.name
                                                }
                                            }));
                                    }
                            });
                    },
                    minLength: 1
     	});





    /*
     $("#relation_update").click(function(){		
     
     $.ajax({
     type: "POST",
     url:'/show/relationship/update_relationship_submit',
     data: $("#relationship_info_form").serialize(),
     success: function(msg) {
     alert(msg);
     }
     });
     
     });
     
     
     $("#contact_update").click(function(){		
     
     
     
     
     
     $.ajax({
     type: "POST",
     url:'/show/contact/data_update_submit',
     data: $("form#contact_info").serialize(),
     success: function(data) {
     alert(data);
     }
     });
     
     });
     */




    $("#home_building_name")
            .bind('focus', function() {

                var b_name = $("#home_building_name").val();
                if (b_name.length == 0)
                    $(this).autocomplete("search");

            }).bind('click', function() {

        var b_name = $("#home_building_name").val();
        if (b_name.length == 0)
            $(this).autocomplete("search");

    })
            .autocomplete({
                minLength: 0,
                source: function(request, response) {
                                    $.ajax({
                                            url: "/client/building_name_josn",
                                            dataType: "json",
                                            data: {
                            query: request.term
                                            },
                                            success: function(data) {
                                                    response($.map(data, function(item) {
                                                        return {
                                    label: item.value,
                                    value: item.value
                                                        }
                                                    }));
                                            }
                                    });
                    },
                focus: function() {
                    return false;
                }
            });

    $("#home_building_name1")
            .bind('focus', function() {
                var b_name1 = $("#home_building_name1").val();
                if (b_name1.length == 0) {
                    $(this).autocomplete("search");
                }
            }).bind('click', function() {

        var b_name1 = $("#home_building_name1").val();
        if (b_name1.length == 0) {
            $(this).autocomplete("search");
        }
    })
            .autocomplete({
                minLength: 0,
                source: function(request, response) {
                                    $.ajax({
                                            url: "/client/building_name_josn",
                                            dataType: "json",
                                            data: {
                            query: request.term
                                            },
                                            success: function(data) {
                                                    response($.map(data, function(item) {
                                                        return {
                                    label: item.value,
                                    value: item.value
                                                        }
                                                    }));
                                            }
                                    });
                    },
                focus: function() {
                    return false;
                }
            });



    $("#home_building_name2")
            .bind('focus', function() {

                var b_name2 = $("#home_building_name2").val();
                //alert(b_name2.length);

                //alert('qweqwe');
                if (b_name2.length == 0) {
                    $(this).autocomplete("search");
                }

            }).bind('click', function() {

        var b_name2 = $("#home_building_name2").val();
        if (b_name2.length == 0) {
            $(this).autocomplete("search");
        }
    })
            .autocomplete({
                minLength: 0,
                source: function(request, response) {
                                    $.ajax({
                                            url: "/client/building_name_josn",
                                            dataType: "json",
                                            data: {
                            query: request.term
                                            },
                                            success: function(data) {
                                                    response($.map(data, function(item) {
                                                        return {
                                    label: item.value,
                                    value: item.value
                                                        }
                                                    }));
                                            }
                                    });
                    },
                focus: function() {
                    return false;
                }
            });
    $("#company_building_name")
            .bind('focus', function() {
                $(this).autocomplete("search");
            }).bind('click', function() {
        $(this).autocomplete("search");
    })
            .autocomplete({
                minLength: 0,
                source: function(request, response) {
                                    $.ajax({
                                            url: "/client/building_name_josn",
                                            dataType: "json",
                                            data: {
                            query: request.term
                                            },
                                            success: function(data) {
                                                    response($.map(data, function(item) {
                                                        return {
                                    label: item.value,
                                    value: item.value
                                                        }
                                                    }));
                                            }
                                    });
                    },
                focus: function() {
                    return false;
                }
            });
    $("#company_building_name1")
            .bind('focus', function() {
                $(this).autocomplete("search");
            }).bind('click', function() {
        $(this).autocomplete("search");
    })
            .autocomplete({
                minLength: 0,
                source: function(request, response) {
                                    $.ajax({
                                            url: "/client/building_name_josn",
                                            dataType: "json",
                                            data: {
                            query: request.term
                                            },
                                            success: function(data) {
                                                    response($.map(data, function(item) {
                                                        return {
                                    label: item.value,
                                    value: item.value
                                                        }
                                                    }));
                                            }
                                    });
                    },
                focus: function() {
                    return false;
                }
            });
    $("#company_building_name2")
            .bind('focus', function() {
                $(this).autocomplete("search");
            }).bind('click', function() {
        $(this).autocomplete("search");
    })
            .autocomplete({
                minLength: 0,
                source: function(request, response) {
                                    $.ajax({
                                            url: "/client/building_name_josn",
                                            dataType: "json",
                                            data: {
                            query: request.term
                                            },
                                            success: function(data) {
                                                    response($.map(data, function(item) {
                                                        return {
                                    label: item.value,
                                    value: item.value
                                                        }
                                                    }));
                                            }
                                    });
                    },
                focus: function() {
                    return false;
                }
            });


    $( "#category" ).click(function(){

     $(this).autocomplete("search"); 


}).autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/category_josn",
                    dataType: "json",
                    data:{
                        query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
                        label: item.value,
                        value: item.value
                        }
                        }));
                    }
          
                });
        },
        focus: function() {
            //alert('aaa');
          return false;
        }
        ,
        select: function( event, ui ) {
            var terms=new Array();
            if(this.value!="")
                terms = this.value.split(", ");
            terms.pop();
            terms.push( ui.item.value );
            terms.push( "" );
            this.value = terms.join( ", " );
            return false;
        }
      });

    $( "#category1" ).click(function(){

     $(this).autocomplete("search"); 


}).autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/category_josn",
                    dataType: "json",
                    data:{
                        query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
                        label: item.value,
                        value: item.value
                        }
                        }));
                    }
          
                });
        },
        focus: function() {
            //alert('aaa');
          return false;
        }
        ,
        select: function( event, ui ) {
            var terms=new Array();
            if(this.value!="")
                terms = this.value.split(", ");
            terms.pop();
            terms.push( ui.item.value );
            terms.push( "" );
            this.value = terms.join( ", " );
            return false;
        }
      });

    $( "#category2" ).click(function(){

     $(this).autocomplete("search"); 


}).autocomplete({
        minLength: 0,
        source: function(request, response ) {
                $.ajax({
                    url: "/client/category_josn",
                    dataType: "json",
                    data:{
                        query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
                        return {
                        label: item.value,
                        value: item.value
                        }
                        }));
                    }
          
                });
        },
        focus: function() {
            //alert('aaa');
          return false;
        }
        ,
        select: function( event, ui ) {
            var terms=new Array();
            if(this.value!="")
                terms = this.value.split(", ");
            terms.pop();
            terms.push( ui.item.value );
            terms.push( "" );
            this.value = terms.join( ", " );
            return false;
        }
      });

    $("#original_broker").autocomplete({
                    source: function(request, response) {
                            $.ajax({
                                    url: "/client/users_josn",
                                    dataType: "json",
                                    data: {
                    query: request.term
                                    },
                                    success: function(data) {
                                            response($.map(data, function(item) {
                                                return {
                            label: item.name,
                            value: item.name
                                                }
                                            }));
                                    }
                            });
                    },
                    minLength: 1
     	});

    $("#broker").autocomplete({
                    source: function(request, response) {
                            $.ajax({
                                    url: "/client/users_josn",
                                    dataType: "json",
                                    data: {
                    query: request.term
                                    },
                                    success: function(data) {
                                            response($.map(data, function(item) {
                                                return {
                            label: item.name,
                            value: item.name
                                                }
                                            }));
                                    }
                            });
                    },
                    minLength: 1
     	});


    $("#company_name").autocomplete({
                    source: function(request, response) {
                            $.ajax({
                                    url: "/client/company_name_josn",
                                    dataType: "json",
                                    data: {
                    query: request.term
                                    },
                                    success: function(data) {
                                            response($.map(data, function(item) {
                                                return {
                            label: item.company_name,
                            value: item.company_name,
                            type_of_company: item.type_of_company,
                            unicode: item.unicode,
                            category: item.category,
                            company_address: item.company_address,
                            company_building_name: item.company_building_name,
                            company_send_publication: item.company_send_publication,
                            company_website: item.company_website

                                                }
                                            }));
                                    }
                            });
                    },
        select: function(e, ui) {
            $("#type_of_company").val(ui.item.type_of_company);
            $("#unicode").val(ui.item.unicode);
            $("#category").val(ui.item.category);
            $("#company_address").val(ui.item.company_address);
            $("#company_building_name").val(ui.item.company_building_name);
            $("#company_send_publication").val(ui.item.company_send_publication);
            $("#company_website").val(ui.item.company_website);
        },
                    minLength: 1
     	});


    $("input").keypress(function(e) {
        var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
        if (keyCode == 13) {
            var i;
            for (i = 0; i < this.form.elements.length; i++) {
                if (this == this.form.elements[i])
                    break;
            }
            i = (i + 1) % this.form.elements.length;
            this.form.elements[i].focus();
            return false;
        } else {
            return true;
        }
    });

    $("select").keypress(function(e) {
        var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
        if (keyCode == 13) {
            var i;
            for (i = 0; i < this.form.elements.length; i++) {
                if (this == this.form.elements[i])
                    break;
            }
            i = (i + 1) % this.form.elements.length;
            this.form.elements[i].focus();
            return false;
        } else {
            return true;
        }
    });

});